const fs = require('fs')
const path = require('path')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const Mock = require('./mock')
let { version } = require('./package.json')

const pages = {}
const modules = fs.readdirSync(path.join(__dirname, './src/modules'))
modules.forEach(filename => {
    const p =  `./src/modules/${filename}/main.js`
    try {
        const stat = fs.readSync(path.join(__filename, p))
        if (stat.isFile()) {
            pages[filename] = p
        }
    } catch (e) {
        console.error(e)
    }
})

module.exports = {
    pages,
    productionSourceMap: false,
    publicPath: process.env.NODE_ENV === 'development' ? '/' : './',
    devServer: {
        before(app){
            Mock(app)
        },
        proxy: {
            '/base/*': {
                target:'http://jsonplaceholder.typicode.com', // IP地址
            }
        }
    },
    chainWebpack: (config) => {
        if (process.env.NODE_ENV === 'development') {
            config.devtool('source-map')
        }
        if (process.env.NODE_ENV !== 'development') {
            config.optimization.minimize('source-map')
        }
    },
    configureWebpack: {
        output: {
            filename: `[name].${version}.js`,
            chunkFilename: `[name].${version}.js`
        },
        plugins: [
            new CompressionWebpackPlugin({
                filename: 'path.gz[query]',
                algorithm: 'gzip',
                test: /\.js$|\.css$/,
                threshold: 10240,
                minRatio: 0.8
            })
        ]
    }
}